/**
 * 接口：行为的抽象，对象的形状，
 * */

//对象的形状
namespace a {
    interface Speakable {
        name: string;

        //任意属性，key（随便起的）是字符串即可（额外属性检查）
        [key: string]: any;

        //只读属性
        readonly PI: 3.1415926

        speak(): void

    }

    let Person: Speakable = {
        name: 'ygg',
        speak() {
        },
        PI: 3.1415926
    }
}


//行为的抽象
//类接口，类的类型
namespace b {
    //用接口去约束了类
    interface Speakable {
        name: string;

        speak(word: string): void
    }

    interface Eatable {
        name: string;

        eat(): void
    }

    //alt + enter可以看到提示信息
    class Person implements Speakable, Eatable {
        eat(): void {
            console.log('eat')
        }

        speak(word: string): void {
            console.log('speak')
        }

        name: string;
    }
}

//函数接口类型
namespace c {
    //两种定义函数接口的写法
    // type Cost =(price: number) => number;
    interface Cost {
        (price: number): number
    }

    let cost: Cost = function (price: number): number {
        return price * .6
    }


    interface Person {
        cost: (price: number) => number   //注意这里区别
    }

    let p: Person = {
        cost
    }


}


//可索引接口：对数组和对象进行约束的，
namespace d {
    interface UserInterface {
        [index: number]: string
    }

    let arr: UserInterface = ['1', '2'];
    let obj: UserInterface = {0: '1', 1: '2'}
}

namespace e {
    class Animal {
        public age: number;
    }
}


//构造器部分的接口类型和实例的接口类型
interface ClockInterface {
    tick()
}

interface ClockConstructor {
    new(hour: number, minute: number): ClockInterface
}

//函数中使用
function createClock(ctor: ClockConstructor, hour: number, minute: number): ClockInterface {
    return new ctor(hour, minute)
}

//类中使用
class DigitalClock implements ClockInterface {
    constructor(h: number, m: number) {

    }

    tick() {
    }
}


//继承接口   (多个接口之前可以共享属性)
interface A {
    A: string
}

interface B {
    B: number
}

interface C extends A, B {
    C: number
}

let item = {} as C;   //强制类型断言为C类型
item.A = 'ygg';
item.B = 3;


//接口继承类   (平时不经常用，用于构建大型工程)
class Control  {
    private state:any

}
interface SelectableControl extends Control{
    select()
}
class Button extends Control implements SelectableControl{
    select() {
    }

}
class Textb extends Control{
 select() {

 }
}
class ImageC implements SelectableControl{
    private state: any;

    select() {
    }

}









